package com.hgx.su.common.springboot.mybatis.plus.config.util;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hgx.su.common.pure.dto.CommonPageReqDto;
import com.hgx.su.common.pure.dto.CommonPageResDto;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Description: PageHelper工具
 * author: HGX
 * DateTime: 2021-02-05 9:26
 */
public class PageHelperUtil {

    /**
     * 分页开始
     *
     * @param pageReqDto 公共分页请求
     * @param <T>        返回类型
     * @return 分页
     */
    public static <T> Page<T> pageStart(CommonPageReqDto pageReqDto) {
        return PageHelper.startPage(pageReqDto.getPageNum(), pageReqDto.getPageSize());
    }

    /**
     * pageHelper 分页结果信息转换成公共分页bean
     *
     * @param pageInfo 分页结果
     * @param data     分页真正内容信息
     * @param <T>      返回内容类型
     * @return 公共分页bean
     */
    public static <T> CommonPageResDto<T> converterToCommonPageDto(PageInfo<?> pageInfo, List<T> data) {
        CommonPageResDto<T> commonPageResDto = new CommonPageResDto<>();
        commonPageResDto.setEndRow(pageInfo.getEndRow());
        commonPageResDto.setBlnFirstPage(pageInfo.isIsFirstPage());
        commonPageResDto.setBlnLastPage(pageInfo.isIsLastPage());
        commonPageResDto.setHasNextPage(pageInfo.isHasNextPage());
        commonPageResDto.setHasPreviousPage(pageInfo.isHasPreviousPage());
        commonPageResDto.setList(data);
        commonPageResDto.setNavigateFirstPage(pageInfo.getNavigateFirstPage());
        commonPageResDto.setNavigateLastPage(pageInfo.getNavigateLastPage());
        commonPageResDto.setNavigatePageNums(Arrays.stream(pageInfo.getNavigatepageNums()).boxed().collect(Collectors.toList()));
        commonPageResDto.setNavigatePages(pageInfo.getNavigatePages());
        commonPageResDto.setNextPage(pageInfo.getNextPage());
        commonPageResDto.setPageNum(pageInfo.getPageNum());
        commonPageResDto.setPages(pageInfo.getPages());
        commonPageResDto.setPageSize(pageInfo.getPageSize());
        commonPageResDto.setPrePage(pageInfo.getPrePage());
        commonPageResDto.setSize(pageInfo.getSize());
        commonPageResDto.setStartRow(pageInfo.getStartRow());
        commonPageResDto.setTotal(pageInfo.getTotal());
        return commonPageResDto;
    }


    public static <T> CommonPageResDto<T> converterToCommonPageDto(PageInfo<T> pageInfo) {
        return converterToCommonPageDto(pageInfo, pageInfo.getList());
    }

    public static <T> CommonPageResDto<T> converterToCommonPageDto(Page<T> page) {
        PageInfo<T> tPageInfo = page.toPageInfo();
        return converterToCommonPageDto(tPageInfo);
    }

    public static <T> CommonPageResDto<T> converterToCommonPageDto(Page<T> page, Integer navigatePages) {
        PageInfo<T> tPageInfo = new PageInfo<>(page, navigatePages);
        return converterToCommonPageDto(tPageInfo);
    }


    public static <T> CommonPageResDto<T> converterToCommonPageDto(Page<?> page, List<T> data) {
        PageInfo<?> tPageInfo = page.toPageInfo();
        return converterToCommonPageDto(tPageInfo, data);
    }


    public static <T> CommonPageResDto<T> converterToCommonPageDto(CommonPageResDto<?> commonPageResDto, List<T> data) {
        CommonPageResDto<T> newCommonPage = new CommonPageResDto<>();
        newCommonPage.setEndRow(commonPageResDto.getEndRow());
        newCommonPage.setBlnFirstPage(commonPageResDto.getBlnLastPage());
        newCommonPage.setBlnLastPage(commonPageResDto.getBlnFirstPage());
        newCommonPage.setHasNextPage(commonPageResDto.getHasPreviousPage());
        newCommonPage.setHasPreviousPage(commonPageResDto.getHasNextPage());
        newCommonPage.setNavigateFirstPage(commonPageResDto.getNavigateFirstPage());
        newCommonPage.setNavigateLastPage(commonPageResDto.getNavigateLastPage());
        newCommonPage.setNavigatePageNums(commonPageResDto.getNavigatePageNums());
        newCommonPage.setNavigatePages(commonPageResDto.getNavigatePages());
        newCommonPage.setNextPage(commonPageResDto.getNextPage());
        newCommonPage.setList(data);
        newCommonPage.setPageSize(commonPageResDto.getPageSize());
        newCommonPage.setPrePage(commonPageResDto.getPrePage());
        newCommonPage.setPageNum(commonPageResDto.getPageNum());
        newCommonPage.setPages(commonPageResDto.getPages());
        newCommonPage.setSize(commonPageResDto.getSize());
        newCommonPage.setStartRow(commonPageResDto.getStartRow());
        newCommonPage.setTotal(commonPageResDto.getTotal());

        return newCommonPage;
    }

}
